package com.zzh.zzhaiagent.rag.local;

import com.zzh.zzhaiagent.domain.entity.UserProfiles;
import com.zzh.zzhaiagent.mapper.UserProfilesMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.markdown.MarkdownDocumentReader;
import org.springframework.ai.reader.markdown.config.MarkdownDocumentReaderConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: zzh
 * @date: 2025/05/07 11:44:32
 * @version: 1.0
 */

//读取本地文档的Loader加载类
@Component
@Slf4j
public class LoveAppDocumentLoader {

    //注入ResourcePatternResolver 用于读取本地文档
    private final ResourcePatternResolver resourcePatternResolver;

    public LoveAppDocumentLoader(ResourcePatternResolver resourcePatternResolver) {
        this.resourcePatternResolver = resourcePatternResolver;
    }

    //读取本地文档
    public List<Document> loadMarkdownDocuments() {
        List<Document> documentList = new ArrayList<>();
        try {
            //读取本地文档
            Resource[] resources = resourcePatternResolver.getResources("classpath:documents/md/*.md");
            for (Resource resource : resources) {
                String filename = resource.getFilename();
                //指定config决定reader如何读取解析文档的规则
                MarkdownDocumentReaderConfig config = MarkdownDocumentReaderConfig.builder()
                        .withHorizontalRuleCreateDocument(false)//是否根据水平线分割文档 -- **
                        .withIncludeBlockquote(true)//是否包含引用块 -- >
                        .withAdditionalMetadata("filename",filename) // 附加元数据 -- filename 增加文档过滤条件
                        .withAdditionalMetadata("documentType","恋爱知识")//附加元数据 -- documentType 增加文档过滤条件
                        .withIncludeCodeBlock(true)//是否包含代码块 -- ``` true 则会读取代码块内容
                        .build();
                //构建Markdown文档对象Reader 进行解析
                MarkdownDocumentReader markdownDocumentReader = new MarkdownDocumentReader(resource, config);
                System.out.println(markdownDocumentReader.get());
                documentList.addAll(markdownDocumentReader.get());
            }
        }catch (Exception e){
            log.error("读取本地文档失败", e);
        }
        return documentList;
    }

    @Autowired
    private UserProfilesMapper userProfilesMapper;

    //读取数据库用户信息，同时转成Document对象
    public List<Document> loadUserDocuments() {
        List<Document> documentList = new ArrayList<>();
        List<UserProfiles> userProfiles = userProfilesMapper.selectList(null);
        //可以优化多线程构建Document对象
        for (UserProfiles userProfile : userProfiles){
//            Integer id = userProfile.getId();
            String name = userProfile.getName();
            Integer age = userProfile.getAge();
            String occupation = userProfile.getOccupation();
            String gender = userProfile.getGender();
            String phone = userProfile.getPhone();
            String hobbies = userProfile.getHobbies();
            String content = String.format("如果您需要找对象的话，这里有一个用户信息：姓名：%s，年龄：%s，职业：%s，性别：%s，手机号：%s，爱好：%s", name, age, occupation, gender, phone, hobbies);
//            String content = String.format("姓名：%s，年龄：%s，职业：%s，性别：%s，手机号：%s，爱好：%s", name, age, occupation, gender, phone, hobbies);
            Document document = new Document(content);//todo 了解Document对象
            document.getMetadata().put("documentType", "兴趣相投的对象"); // 添加文档类型标识
            documentList.add(document);
        }
        return documentList;
    }


}
